package ylg.cn.com.shenshen;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import ylg.cn.com.shenshen.fragment.HomeFragment;
import ylg.cn.com.shenshen.fragment.LocalFragment;
import ylg.cn.com.shenshen.fragment.MineFragment;
import ylg.cn.com.shenshen.fragment.StorageFragment;

public class MainActivity extends AppCompatActivity implements TabHost.OnTabChangeListener{

    private List<TabItem> tabItemList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initTabData();

        initTabView();


    }

    private int index = 0;
    @Override
    public void onTabChanged(String tabId) {
        for (int i = 0 ; i < tabItemList.size() ; i++){
            TabItem tabItem = tabItemList.get(i);
            if (tabItem.getTabText().equals(tabId)){
                tabItem.setChecked(true);
                index = i;
            }else {
                tabItem.setChecked(false);
            }
        }
    }

    private void initTabData(){
        tabItemList = new ArrayList<TabItem>();
        //首页
        tabItemList.add(new TabItem(R.mipmap.home
                ,R.mipmap.home_select,R.string.home, HomeFragment.class));
        //提醒
        tabItemList.add(new TabItem(R.mipmap.storage
                ,R.mipmap.storage_select,R.string.hourse, StorageFragment.class));

        //工作
        tabItemList.add(new TabItem(R.mipmap.local
                ,R.mipmap.local_select,R.string.work, LocalFragment.class));
        //我的
        tabItemList.add(new TabItem(R.mipmap.mine
                ,R.mipmap.mine_select,R.string.mine, MineFragment.class));

    }
    FragmentTabHost fragmentTabHost;
    private void initTabView(){
        //绑定和加载替换我们的Fragment(说白了用来管理我们的Fragment)
        fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        //指定Fragment绑定的布局
        fragmentTabHost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent);
        //删除分割线
        fragmentTabHost.getTabWidget().setDividerDrawable(null);

        for (int i = 0 ; i < tabItemList.size() ; i++){
            TabItem tabItem = tabItemList.get(i);
            //创建Tab
            TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(tabItem.getTabText())
                    .setIndicator(tabItem.getTabIndicator());
            //添加Fragment
            fragmentTabHost.addTab(tabSpec,tabItem.getFragmentClass(),tabItem.getBundle());
            //给我们的Tab设置背景
            fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundColor(getResources().getColor(R.color.tabbar_bottom_bg));
            fragmentTabHost.setOnTabChangedListener(this);
            //设置默认选中Tab
            if (i == 0){
                tabItem.setChecked(true);
            }
        }
    }
    public class TabItem{
        //正常显示的图片
        private int imageNormal;
        //选中图片
        private int imagePress;
        //tab名字
        private int tabTextRes =0;
        //tab对应的Fragment
        private Class<? extends Fragment> fragmentClass;
        //Fragment对应的数据(例如:下标)
        private Bundle bundle;

        private View viewIndicator;
        private ImageView iv_tab;
        private TextView tv_tab;


        public TabItem(int imageNormal,int imagePress,int tabTextRes,Class<? extends Fragment> fragmentClass){
            this.imageNormal = imageNormal;
            this.imagePress = imagePress;
            this.tabTextRes = tabTextRes;
            this.fragmentClass = fragmentClass;
        }

        public int getImagePress() {
            return imagePress;
        }

        public String getTabText(){
            if (tabTextRes == 0){
                return "";
            }
            return getResources().getString(tabTextRes);
        }

        public Class<? extends Fragment> getFragmentClass() {
            return fragmentClass;
        }

        public int getImageNormal() {
            return imageNormal;
        }

        public int getTabTextRes() {
            return tabTextRes;
        }

        public Bundle getBundle(){
            if (bundle == null){
                bundle = new Bundle();
                bundle.putInt("tabTextRes",tabTextRes);
            }
            return bundle;
        }

        //重置Tab样式
        public void setChecked(boolean isChecked){
            if (iv_tab != null){
                if (isChecked){
                    iv_tab.setImageResource(imagePress);
                }else {
                    iv_tab.setImageResource(imageNormal);
                }
            }

            if (tv_tab != null){
                if (isChecked){
                    tv_tab.setTextColor(getResources().getColor(R.color.main_bottom_text_select));
                }else {
                    tv_tab.setTextColor(getResources().getColor(R.color.main_bottom_text_normal));
                }
            }

        }

        public View getTabIndicator(){
            //创建Tab视图
            if (viewIndicator == null) {
                int layoutId = 0;
                //判断加载哪一个布局文件
                if (this.tabTextRes <= 0) {
                    layoutId = R.layout.tabbar_publish_indicator;
                } else {
                    layoutId = R.layout.view_tab_indicator;
                }
                viewIndicator = getLayoutInflater().inflate(layoutId, null);
                iv_tab = (ImageView) viewIndicator.findViewById(R.id.iv_tab);
                if (this.tabTextRes > 0) {
                    tv_tab = (TextView) viewIndicator.findViewById(R.id.tv_tab);
                    tv_tab.setText(getTabText());
                }
                //绑定默认的资源
                iv_tab.setImageResource(imageNormal);
            }
            return viewIndicator;
        }
    }
}
